package tuiLayer;
import controlLayer.LoanCtr;
import java.util.ArrayList;
import java.util.Scanner;

public class LoanMenuUI
{
    
    private LoanCtr controller;
    private Scanner scanner;
    
    public LoanMenuUI()
    {
        controller = new LoanCtr();
        scanner = new Scanner(System.in);
    }
    
    public void start()
    {
        System.out.println("This is the loans menu:");
        System.out.println("1. Create Loan");
        System.out.println("2. Return Copy");
        System.out.println("3. Back");
        processInput();
    }
    
    private void processInput()
    {
        
       int input = Integer.parseInt(scanner.nextLine());
       switch(input)
       {
           case 1:
                addLoan();
                start();
           break;
           case 2:
                returnLoan();
                start();
           break;
           case 3:
           break;
           default:
            System.out.println("invalid input");
            processInput();
           break;
        }
    }
    
    private void returnLoan()
    {
        System.out.println("serial number:");
        String serialNumber = scanner.nextLine();
        controller.returnCopy(serialNumber);
    }
    
    private void addLoan()
    {
        ArrayList<String> serialNumbers = new ArrayList<String>();
        
        System.out.println("Friend phoneNumber:");
        String phoneNumber = scanner.nextLine();
        
        System.out.println("Period:");
        String period = scanner.nextLine();
        
        boolean done = false;
        
        while(!done)
        {
            System.out.println("serial number:");
            String serialNumber = scanner.nextLine();
            if(controller.copyIsLentOut(serialNumber)){
                System.out.println("Copy is already lent out.");
            }else{
            serialNumbers.add(serialNumber);
        }
            done = done();

        }
        controller.createLoan(phoneNumber, serialNumbers, period);
    }
    
    private boolean done()
    {
            System.out.println("Are you done? Y/N?");
            
            switch(scanner.nextLine().toLowerCase())
            {
                case "y":
                return true;
                case "n":
                return false;
                default:
                return done();
            }
    }
}
